package com.itheima.test;

import java.util.Scanner;

public class test1case2 {
    public static void main(String[] args) {
        //键盘录入字符串，要求长度《=9，只能是数字
        //将内容变成罗马数字

        //键盘录入
        Scanner sc = new Scanner(System.in);
        //String str = "12345a";
        String str;
        while (true) {
            System.out.println("请输入一个字符串：");
            str = sc.next();

            //校验是否满足要求
            boolean flag = checkStr(str);
            if(flag){
                break;
            }else{
                System.out.println("字符串不满足要求,，请重新输入");
                continue;
            }
        }

        //将字符串变成罗马数字，查表法
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            //System.out.println(c);
            String s = getRoman(c);
            //System.out.println(s);
            //拼接
            sb.append(s);
        }

        System.out.println(sb.toString());


    }

    //匹配法
    public static String getRoman(char num) {
        String str;
        str = switch(num){ //str写在switch前面，后面加；
            /*case '0'->str = "";
            case '1'->str = "I";
            case '2'->str = "II";
            case '3'->str = "III";
            case '4'->str = "IV";
            case '5'->str = "V";
            case '6'->str = "VI";
            case '7'->str = "VII";
            case '8'->str = "VIII";
            case '9'->str = "IX";
            default -> str = "";*/
            case '0'->"";
            case '1'->"I";
            case '2'->"II";
            case '3'->"III";
            case '4'->"IV";
            case '5'->"V";
            case '6'->"VI";
            case '7'->"VII";
            case '8'->"VIII";
            case '9'->"IX";
            default -> "";

        };
        return str;
    }

    public static boolean checkStr(String str) {
        //要求1：长度<=9
        if(str.length()>9){
            return false;
        }

        //要求2：只能是数字
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(ch<'0' || ch>'9'){
                return false;
            }
        }
        return true;
    }
}
